package com.huluobo.a2201mvvm_imdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LC on 2024/4/2.
 * 自定义字母条
 */
public class SlideBar extends View {

    //所有字母的集合
    private List<String> SECTIONS = new ArrayList<>();

    //画笔
    private Paint paint;

    //一个字母的高度
    private float sectionHeight = 0;

    //定义基准线
    private float textBaseline = 0;

    public SlideBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    //初始化字母条的方法,需要填入字母数据和初始化画笔
    private void init() {
        //填入26个字母
        SECTIONS.add("搜");
        SECTIONS.add("A");
        SECTIONS.add("B");
        SECTIONS.add("C");
        SECTIONS.add("D");
        SECTIONS.add("E");
        SECTIONS.add("F");
        SECTIONS.add("G");
        SECTIONS.add("H");
        SECTIONS.add("I");
        SECTIONS.add("J");
        SECTIONS.add("K");
        SECTIONS.add("L");
        SECTIONS.add("M");
        SECTIONS.add("N");
        SECTIONS.add("O");
        SECTIONS.add("P");
        SECTIONS.add("Q");
        SECTIONS.add("R");
        SECTIONS.add("S");
        SECTIONS.add("T");
        SECTIONS.add("U");
        SECTIONS.add("V");
        SECTIONS.add("W");
        SECTIONS.add("X");
        SECTIONS.add("Y");
        SECTIONS.add("Z");
        SECTIONS.add("#");

        //初始化画笔
        paint = new Paint();
        paint.setColor(Color.GRAY);
        paint.setTextSize(30);
        paint.setTextAlign(Paint.Align.CENTER);//设置绘制文字的位置,以起点为中心
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //当view的尺寸发生改变时,重新计算每个字母的高度
        //h 代表改变后整个view的高度
        sectionHeight = h * 1f / SECTIONS.size();
        //计算基准线
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();//获取文字的尺寸
        float textHeight = fontMetrics.descent - fontMetrics.ascent;//获取一个字符的高度
        textBaseline = sectionHeight / 2 + (textHeight / 2 - fontMetrics.descent);//基准线计算公式
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawText(SECTIONS.get(0), x / 2, sectionHeight, paint);
        float x = getWidth() * 1.0f / 2;//设置绘制x轴的位置,当前位置为view的宽度的一半
//        float y = sectionHeight;//绘制y轴的位置
        float baseline = textBaseline;

        //循环整个字符集合,绘制每一个字符
        for (int i = 0; i < SECTIONS.size(); i++) {
            canvas.drawText(SECTIONS.get(i), x, baseline, paint);
            //绘制的高度需要++,因为绘制一个字符后,需要在他的下方绘制下一个字符
            baseline += sectionHeight;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //手指按下的事件,修改背景颜色为灰色
                setBackgroundColor(Color.DKGRAY);
                onSectionChangeListener.onSectionChange(SECTIONS.get(getTouchIndex(event)));
                break;
            case MotionEvent.ACTION_MOVE:
                onSectionChangeListener.onSectionChange(SECTIONS.get(getTouchIndex(event)));
                break;
            case MotionEvent.ACTION_UP:
                setBackgroundColor(Color.TRANSPARENT);
                onSectionChangeListener.onSlideFinish();
                //抬起回复透明
                break;
        }
        return true;//只要触摸到当前view上,就消费掉事件,不让事件再向下传递,不让事件再返回到后方的RecyclerView上
    }

    private int getTouchIndex(MotionEvent event) {
        int index = (int) (event.getY() / sectionHeight);//当前触摸的高度(Y值),除以一个字母的噶度,就能算出它的下标
        if (index < 0) {
            index = 0;
        } else if (index >= SECTIONS.size()) {
            index = SECTIONS.size() - 1;
        }
        return index;
    }

    public interface OnSectionChangeListener {
        void onSectionChange(String firstLetter);//回调出字符

        void onSlideFinish();//当手指抬起的时候的回调,为了让字母消失
    }

    private OnSectionChangeListener onSectionChangeListener;

    public void setOnSectionChangeListener(OnSectionChangeListener onSectionChangeListener) {
        this.onSectionChangeListener = onSectionChangeListener;
    }
}
